package com.whyt.oauth.consumer.controller;

import com.whyt.oauth.consumer.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * @author ASUS
 */
@RestController
public class ConsumerController
{
    @Autowired
    private ProviderService providerService;

    @Value("${test}")
    private String test;

    @GetMapping("hello")
    public String hello(String name)
    {
        return providerService.sayHello(name);
    }

    @GetMapping("testAdmin")
    @PreAuthorize("hasAuthority('ADMIN')")
    public String testAdmin()
    {
        return "testAdmin:" + test;
    }

    @GetMapping("testUser")
    @PreAuthorize("hasAuthority('USER')")
    public String testUser()
    {
        return "testUser:" + test;
    }

    @GetMapping("testAdminOrUser")
    @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('USER')")
    public String testAdminOrUser()
    {
        return "testAdminOrUser:" + test;
    }

    @GetMapping(path = "/user/details", produces = "application/json" )
    public Principal userDetails(Principal principal)
    {
        return principal;
    }
}
